<?php
set_time_limit(0);
include_once 'simple_html_dom.php';
function fun($w){
	$url="http://dict.youdao.com/search?le=eng&q=lj%3A".$w."&keyfrom=dict.index";
	$data=array();
	$html= file_get_html($url);
	$base =$html->find("#bilingual",0);
	if($base->children(0)->tag=="ul"){
		$bbase=$base->children(0);
	}elseif($base->children(1)->tag=="ul"){
		$bbase=$base->children(1);
	}else{
		echo("!!!").$w;
		return null;
	}
	foreach ($bbase->children() as $r){
		$data[]=array('e'=> strip_tags($r->children(0)->innertext),
		'c'=> strip_tags($r->children(1)->innertext),
		'r'=> strip_tags($r->children(2)->innertext)
		 );
	};//print_r($data);
	return $data;
}
mysql_connect("localhost","root","");
mysql_select_db("diandi_danci");
mysql_query("set names utf8");
$result = mysql_query("select * from word ");
while($row = mysql_fetch_array($result)){
	echo $row['id']."<br>\n";
	$data = fun($row['word']);//continue;
	if(empty($data))continue;
	foreach ($data as $lj){
		
		$sql="select * from example where english='".mysql_escape_string($lj['e'])."'";
	
		$re = mysql_query($sql);
		if ($r=mysql_fetch_array($re)){
			mysql_query("update example set words='".$r['words'].$row['id'].","."' where id=".$r['id']);
		}else{
			$sql =
					"INSERT INTO `diandi_danci`.`example` (`id`, `english`, `chinese`,reference, `words`) 
					VALUES (NULL, '".mysql_escape_string($lj['e'])."', '".mysql_escape_string($lj['c'])."','".mysql_escape_string($lj['r'])."', '".$row['id'].",')";
			mysql_query($sql);
		}
		
	}
	
	
}